<?php
/**
 * 会员管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/3
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\BankModel;
use app\common\model\MemberModel;
use app\common\model\MessageModel;
use app\common\model\UserRefundModel;
use think\facade\Request;

class MemberController extends AdminBaseController
{
    /**
     * 用户列表
     */
    public function index(MemberModel $m)
    {

        $data = Request::param("");
        $where = $this->search($data);

        $list = $m->getALL($where);

        $this->assign("list", $list);
        $this->assign("gd", $data);
        return $this->fetch();
    }

    /**
     * 用户列表数据导出
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function user_export(){
        $name='用户列表';
        $tit=['ID','手机号','昵称','推荐码','佣金余额','支付宝收款账号','微信收款账号','注册来原','注册时间','最后登陆时间','最后登陆IP','状态'];

        $param = Request::param("");
        $where = $this->search($param);

        $arr=MemberModel::getSelect($where);

        $data=[];
        foreach ($arr as $key=>$info){
            $data[$key]['ID']=$info['uid'];
            $data[$key]['mobile']=$info['mobile'];
            $data[$key]['nickname']=$info['nickname'];
            $data[$key]['invite_code']=$info['invite_code'];
            $data[$key]['distribut_money']=$info['distribut_money'];
            $data[$key]['alipay_user']=$info['alipay_user'];
            $data[$key]['wechat_user']=$info['wechat_user'];
            $data[$key]['oauth']=$info['oauth'];
            $data[$key]['create_time']=$info['create_time'];
            $data[$key]['last_login']=date('Y-m-d H:i:s',$info['last_login']);
            $data[$key]['last_ip']=$info['last_ip'];
            $data[$key]['status']=$info['status']?'正常':'禁用';
        }
        exportExcel($name,$tit,$data);
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data)
    {

        $where[] = ["status", ">", MemberModel::STATUS_DEL];

        if (!empty($data["uid"])) {
            $where[] = ["uid", "=", $data["uid"]];
        }

        if (!empty($data["mobile"])) {
            $where[] = ["mobile", 'like', "%{$data['mobile']}%"];
        }

        return $where;
    }

    /**
     * 增加/编辑
     * @param int $id
     * @param int $pid
     * @return mixed
     */
    public function edit($id = 0)
    {
        if (Request::isPost()) {
            $data = Request::param();

            $AM = new MemberModel();
            $status = $AM->addUser($data);

            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }
        } else {
            $info = MemberModel::get($id);


            $this->assign("info", $info);
            $this->assign("id", $id);
            return $this->fetch();
        }
    }

    /**
     * 查看详情
     * @param int $id
     * @param MemberModel $m
     * @return mixed
     */
    public function detail($id = 0,$type = 0, MemberModel $m)
    {
        $info = $m::get($id);

        $this->assign("info", $info);
        $this->assign("type", $type);
        return $this->fetch();
    }

    /**
     * 状态修改
     */
    public function acStatus()
    {
        $id = Request::post("id");
        $status = Request::post("status");

        $am = MemberModel::get($id);
        $am->status = $status;
        $st = $am->save();

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    public function inviteStatus(){
        $id = Request::post("id");
        $status = Request::post("status");

        $am = MemberModel::get($id);
        $am->is_invite = $status;
        $st = $am->save();

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }


    /**
     * 删除分类
     */
    public function del(MemberModel $m)
    {
        $id = Request::post("id");

        $status = $m->del($id);

        if ($status['status']) {
            $this->success($status['msg']);
        } else {
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除
     */
    public function delAll(MemberModel $m)
    {
        $id = Request::post("ids");

        $error = 0;
        $sussuc = 0;
        foreach ($id as $vo) {
            $status = $m->del($vo);
            if (!$status['status']) {
                $error++;
                continue;
            } else {
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 修改用户密码
     * @return mixed
     */
    public function update_pwd($id = 0)
    {
        if (Request::isPost()) {
            $uid = Request::param("uid");
            $new_pwd = Request::param("new_pwd");
            $real_pwd = Request::param("real_pwd");

            if (empty($new_pwd) || $new_pwd == "") {
                $this->error("新密码不能为空！");
            }

            if ($real_pwd != $new_pwd) {
                $this->error("重复密码不正确！");
            }

            $au = MemberModel::get($uid);

            $au->password = password($real_pwd, $au->salt);
            $status = $au->save();

            if ($status) {
                $this->success("修改成功！");
            } else {
                $this->error("修改失败！");
            }

        } else {
            $this->assign('uid', $id);
            return $this->fetch();
        }
    }

    /**
     * 用户银行卡
     */
    public function bank(BankModel $bank)
    {
        $data = Request::param("");
        $where = $this->bankSearch($data);

        $list = $bank->getALL($where);

        $this->assign("list", $list);
        $this->assign("gd", $data);
        return $this->fetch();
    }

    /**
     * 搜索银行卡
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function bankSearch($data)
    {

        $where = [];

        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }
        return $where;
    }

    /**
     * 删除银行卡
     */
    public function bank_del(BankModel $m)
    {
        $id = Request::post("id");

        $status = $m->del($id);

        if ($status['status']) {
            $this->success($status['msg']);
        } else {
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除银行卡
     */
    public function bank_delAll(BankModel $m)
    {
        $id = Request::post("ids");

        $error = 0;
        $sussuc = 0;
        foreach ($id as $vo) {
            $status = $m->del($vo);
            if (!$status['status']) {
                $error++;
                continue;
            } else {
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 用户提现列表
     */
    public function refund(UserRefundModel $ur)
    {
        $data = Request::param("");
        $where = $this->refundSearch($data);

        $list = $ur->getALL($where);

        $this->assign("list", $list);
        $this->assign("gd", $data);
        return $this->fetch();
    }

    /**
     * 搜索银行卡
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function refundSearch($data)
    {

        $where = [];

        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }

        if (!empty($data["status"]) && $data["status"] != -2) {


            $where[] = ["status", '=', $data['status'] - 1];
        }

        return $where;
    }

    /**
     * 删除银行卡
     */
    public function refund_del(UserRefundModel $m)
    {
        $id = Request::post("id");

        $status = $m->del($id);

        if ($status['status']) {
            $this->success($status['msg']);
        } else {
            $this->error($status['msg']);
        }
    }

    /**
     * 批量删除银行卡
     */
    public function refund_delAll(UserRefundModel $m)
    {
        $id = Request::post("ids");

        $error = 0;
        $sussuc = 0;
        foreach ($id as $vo) {
            $status = $m->del($vo);
            if (!$status['status']) {
                $error++;
                continue;
            } else {
                $sussuc++;
            }
        }

        $this->success("成功删除{$sussuc}条，删除失败{$error}条");
    }

    /**
     * 提现详情
     * @param int $id
     * @param UserRefundModel $m
     * @return mixed
     */
    public function refund_detail($id = 0, UserRefundModel $m)
    {
        $info = $m->getOne($id);

        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * 提现处理
     * @param int $id
     * @param UserRefundModel $m
     * @return mixed
     */
    public function refund_action($id = 0)
    {

        if (Request::isPost()) {
            $memo = Request::param("memo");
            $id = Request::param("id");

            $au = UserRefundModel::get($id);
            $au->status = UserRefundModel::SHOW_STATUS;
            $au->refund_finish_time = time();
            $au->memo = $memo;
            $au->admin_user_id = $this->uid;
            $status = $au->save();

            if ($status) {
                $this->success("处理成功！");
            } else {
                $this->error("处理失败！");
            }

        } else {
            $this->assign("id",$id);
            return $this->fetch();
        }
    }

    /**
     * 会员提现申请数据导出
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function refund_export(){
        $name='会员提现申请';
        $tit=['ID','会员','提现金额','提现手续费','实际到账金额','提现到','申请时间','处理时间','状态','申请来源','操作管理员','支付宝账号','微信账号','银行卡户主名','银行名称','银行账号','支付行','备注'];

        $param = Request::param("");
        $where = $this->refundSearch($param);

        $arr=UserRefundModel::getSelect($where);

        $data=[];
        foreach ($arr as $key=>$info){
            $data[$key]['ID']=$info['id'];
            $data[$key]['mobile']=$info['user']['mobile'];
            $data[$key]['refund_money']=$info['refund_money'];
            $data[$key]['refund_system_take']=$info['refund_system_take'];
            $data[$key]['refund_price']=$info['refund_price'];
            $data[$key]['refund_to']=arrText($info['refund_to'],['wechat'=>'微信','alipay'=>'支付宝','bank'=>'银行卡']);
            $data[$key]['create_time']=date('Y-m-d H:i:s',$info['create_time']);
            $data[$key]['refund_finish_time']=date('Y-m-d H:i:s',$info['refund_finish_time']);
            $data[$key]['status']=$info['status']?'已提现':'申请中';
            $data[$key]['refund_type']=$info['refund_type'];
            $data[$key]['nickname']=$info['admin']['nickname'];
            $data[$key]['wechat_user']=$info['user']['wechat_user'];
            $data[$key]['alipay_user']=$info['user']['alipay_user'];

            if(!empty($info['user'])){
                $data[$key]['bank_real_name']=$info['bank']['bank_real_name'];
                $data[$key]['bank_name']=$info['bank']['bank_name'];
                $data[$key]['bank_numb']=$info['bank']['bank_numb']." ";
                $data[$key]['bank_branch']=$info['bank']['bank_branch'];
            }
        }
        exportExcel($name,$tit,$data);
    }

    /**
     * 平台留言
     * @param MessageModel $m
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function message(MessageModel $m){
        $data = Request::param("");
        $where = $this->refundSearch($data);

        $list = $m->getALL($where);

        $this->assign("list", $list);
        $this->assign("gd", $data);
        return $this->fetch();
    }

    /**
     * 留言详情
     * @param $id
     * @return mixed
     */
    public function msg_detail($id){
        $info=MessageModel::get($id);

        if(!$info->status){
            $info->status=MessageModel::SHOW_STATUS;
            $info->save();
        }

        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 删除留言
     */
    public function msg_del(MessageModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }

    /**
     * 实名审核
     */
    public function realName(){
        $id = Request::post("id");
        $status = Request::post("status");

        $am = MemberModel::get($id);

        if($status==1){
            $am->real_name_status = 1;
            $st = $am->save();
        }else{
            $am->code_front = "";
            $am->code_behind = "";
            $st = $am->save();
        }

        if ($st) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

}